package com.eshop.cart.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.eshop.bean.GoodsBean;
import com.eshop.dao.Conn;
import com.eshop.java.PageUserSearch;



public class UserSearchDao {
	
	private Connection conn = null;
	private PreparedStatement sta = null;
	private ResultSet r = null;
	
	public ArrayList<GoodsBean> userSearch(String usersearch,PageUserSearch p)
	{
		conn = Conn.getConn();
		ArrayList<GoodsBean> usersearchlist = new ArrayList<GoodsBean>();
		/*String sql = " SELECT * from goods where goods_description like '%"+us ersearch+"%' ";*/
		String sql = " SELECT * from goods where goods_description like ? limit ?,?";
		
		try {
			  sta = conn.prepareStatement(sql);
			  sta.setObject(1, "%"+usersearch+"%");
			  sta.setObject(2, p.getStartRow());
			  sta.setObject(3, p.getProws());
			  r = sta.executeQuery();
			  
			  while(r.next())
			  {
				
				 Integer goodsId =  r.getInt("goods_id");
				 Integer brandsId = r.getInt("brands_id");
				 Integer adminsId = r.getInt("admins_id");
				 String goodsName = r.getString("goods_name");
				 String goodsImg =  r.getString("goods_img");
				 Float goodsPrice = r.getFloat("goods_price");
				 Integer goodsStock = r.getInt("goods_stock");
				 String goodsDescription = r.getString("goods_description");
				 Integer goodsStatus = r.getInt("goods_status");
				 Integer goodsExist = r.getInt("goods_exist");
				 
				 GoodsBean goodsBean = new GoodsBean();
				 goodsBean.setGoodsId(goodsId);
				 goodsBean.setBrandsId(brandsId);
				 goodsBean.setAdminsId(adminsId);
				 goodsBean.setGoodsName(goodsName);
				 goodsBean.setGoodsImg(goodsImg);
				 goodsBean.setGoodsPrice(goodsPrice);
				 goodsBean.setGoodsDescription(goodsDescription);
				 goodsBean.setGoodsStatus(goodsStatus);
				 goodsBean.setGoodsExist(goodsExist);
				 goodsBean.setGoodsStock(goodsStock);
				  
				 
				 usersearchlist.add(goodsBean);
			  }
			  
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
					if(r != null)
					{
						r.close();
					}
					if(sta != null)
					{
						sta.close();
					}
					if(conn != null)
					{
						conn.close();
					}
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
	}
		
		
		
		return usersearchlist;
	}

}
